﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.Globalization;
    using System.Xml;

    public sealed class ClientActionSetStaticProperty : ClientAction
    {
        private object m_propValue;
        private ChunkStringBuilder m_sb;
        private SerializationContext m_serializationContext;
        private string m_typeId;

        public ClientActionSetStaticProperty(string typeId, string propName, object propValue) : base(null, propName)
        {
            this.m_typeId = typeId;
            this.m_propValue = propValue;
            this.m_serializationContext = new SerializationContext();
            this.m_sb = new ChunkStringBuilder();
            XmlWriter writer = this.m_sb.CreateXmlWriter();
            this.WriteToXmlPrivate(writer, this.m_serializationContext);
            writer.Close();
            this.m_propValue = null;
        }

        internal override void WriteToXml(XmlWriter writer, SerializationContext serializationContext)
        {
            this.m_sb.WriteContentAsRawXml(writer);
            serializationContext.AddObjectPathsFrom(this.m_serializationContext);
        }

        private void WriteToXmlPrivate(XmlWriter writer, SerializationContext serializationContext)
        {
            writer.WriteStartElement("SetStaticProperty");
            writer.WriteAttributeString("Id", base.Id.ToString(CultureInfo.InvariantCulture));
            writer.WriteAttributeString("TypeId", this.m_typeId);
            writer.WriteAttributeString("Name", base.Name);
            writer.WriteStartElement("Parameter");
            DataConvert.WriteValueToXmlElement(writer, this.m_propValue, serializationContext);
            writer.WriteEndElement();
            writer.WriteEndElement();
        }
    }
}

